import { ATeamCombat } from "GameLoop/WorldState/EntityManager/Entitys";
import { BinaryDecision } from "framework/FSM/DecisionTree/Decision";
import { PositionHelper } from "utils/position/PositionHelper";

 
/**
 * 队伍已经分散了
*/
export class DecisionIsTeamTargetDismiss <T extends IStateArg<ATeamCombat,ITask>> extends BinaryDecision<T>
{

    public testValue(arg:T): boolean
    {
        const creeps = arg.entity.creeps;
        const room_group =_.groupBy(creeps,e=>e.pos.roomName);
        const keys = Object.keys(room_group);
        if(keys.length == 2 )
        {
            if(room_group[keys[0]].length != room_group[keys[1]].length)
            {
                global.Log.Info(`队伍分散了 :${JSON.stringify(_.map(room_group))}`);
                return true;
            }
        }
        for(let  i=0;i<creeps.length-1;i++)
        {
            for(let j=i+1;j<creeps.length;j++)
            {
                const pos1 =creeps[i].pos;
                const pos2 =creeps[j].pos;
                if(pos1 && pos2)
                {
                    const distance = PositionHelper.Distance(pos1,pos2);
                    //global.Log.Info(`${JSON.stringify(pos1)}->${JSON.stringify(pos2)}的 距离 :${distance}`);
                    if(pos1.roomName  == pos2.roomName)
                    {
                        if(distance>1)
                        {
                                // 队形已经破坏
                            return true;
                        }
                    }
                    else
                    {
                        if(distance>2)
                        {
                                // 队形已经破坏
                            return true;
                        }
                    }
                }
                else
                {
                    global.Log.Error(`${JSON.stringify(pos1)}->${JSON.stringify(pos2)}的 坐标错误不存在`);
                    // 队形已经破坏
                    return true;
                }
            }
        }

        return false;
        // return true;
    }

    value: any;


    public setValue(val: any)
    {
        this.value = val;
    }
}

